package com.todo.strategy.online.application.service.pipeline.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.component.redis.RedisService;
import com.todo.strategy.common.constant.StrategyOnlineKeyBuild;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.PlatformStrategyDetailBO;
import com.todo.strategy.online.application.service.pipeline.PStrategyPipelineService;
import com.todo.strategy.online.infrastructure.entity.domain.PlatformStrategyDO;
import com.todo.strategy.online.infrastructure.helper.es.entity.BasicStrategyEntity;
import com.todo.strategy.online.infrastructure.helper.es.mapper.BasicStrategyEntityMapper;
import com.todo.strategy.online.infrastructure.repository.PlatformStrategyRepository;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/3/1 16:07
 * @description
 */
@Slf4j
@Component
public class PStrategyPipelineServiceImpl implements PStrategyPipelineService {

    @Resource
    private PlatformStrategyRepository platformStrategyRepository;
    @Resource
    private RedisService redisService;
    @Resource
    private BasicStrategyEntityMapper strategyEntityMapper;

    @Override
    public Boolean insertPStrategyDetail(Long businessId, String originData) {
        PlatformStrategyDO alreadyData = platformStrategyRepository.queryPlatformStrategyById(businessId);
        if (alreadyData != null) {
            return true;
        }
        PlatformStrategyDO platformStrategyDO = JSONObject.parseObject(originData, PlatformStrategyDO.class);
        Boolean updated = platformStrategyRepository.insertPlatformStrategyDetail(platformStrategyDO);
        if (updated) {
            //新增成功后，我们针对保存完成的数据进行缓存、es的存储
            PlatformStrategyDetailBO platformStrategyDetailBO = StrategyConvert.convertToPlatformStrategyDetailBO(platformStrategyDO);
            redisService.set(StrategyOnlineKeyBuild.strategyPlatformDetailKey(platformStrategyDO.getId()), JSONObject.toJSONString(platformStrategyDetailBO));
            BasicStrategyEntity entity = StrategyConvert.buildBasicStrategyEntity(platformStrategyDetailBO);
            strategyEntityMapper.insert(entity);
        }
        return updated;
    }

    @Override
    public Boolean updatePStrategyDetail(Long businessId, String originData) {
        PlatformStrategyDO alreadyData = platformStrategyRepository.queryPlatformStrategyById(businessId);
        if (alreadyData == null) {
            log.error("PStrategyPipelineService#updatePStrategyDetail error, alreadyData is null, businessId:{}", businessId);
            return false;
        }
        PlatformStrategyDO platformStrategyDO = JSONObject.parseObject(originData, PlatformStrategyDO.class);
        Boolean updated = platformStrategyRepository.updatePStrategyDetail(platformStrategyDO);
        if (updated) {
            redisService.del(StrategyOnlineKeyBuild.strategyPlatformDetailKey(platformStrategyDO.getId()));
            BasicStrategyEntity strategyEntity = StrategyConvert.buildUpdateBasicStrategyEntity(alreadyData, platformStrategyDO);
            LambdaEsQueryWrapper<BasicStrategyEntity> wrapper = new LambdaEsQueryWrapper<>();
            strategyEntityMapper.update(strategyEntity, wrapper);
        }
        return updated;
    }

    @Override
    public Boolean removePStrategyDetail(Long businessId) {
        PlatformStrategyDO alreadyData = platformStrategyRepository.queryPlatformStrategyById(businessId);
        if (alreadyData == null) {
            return true;
        }
        Boolean removed = platformStrategyRepository.removePStrategyDetail(businessId);
        if (removed) {
            redisService.del(StrategyOnlineKeyBuild.strategyPlatformDetailKey(businessId));
            LambdaEsQueryWrapper<BasicStrategyEntity> wrapper = new LambdaEsQueryWrapper<>();
            wrapper.eq(BasicStrategyEntity::getMysqlId, businessId);
            strategyEntityMapper.delete(wrapper);
        }
        return true;
    }

    @Override
    public Boolean enablePStrategyDetail(Long businessId, String originData) {
        PlatformStrategyDO platformStrategyDO = JSONObject.parseObject(originData, PlatformStrategyDO.class);
        if (platformStrategyDO == null || platformStrategyDO.getBaseStrategyInfo() == null) {
            return false;
        }
        PlatformStrategyDO alreadyData = platformStrategyRepository.queryPlatformStrategyById(businessId);
        if (alreadyData == null) {
            return false;
        }
        if (alreadyData.getBaseStrategyInfo().getStatus().equals(platformStrategyDO.getBaseStrategyInfo().getStatus())) {
            return true;
        }
        Boolean updated = platformStrategyRepository.updatePStrategyDetail(platformStrategyDO);
        //如果为关闭或者废弃则剔除缓存和es
        if (updated && (StrategyStatusEnum.DISABLE.getType() == platformStrategyDO.getBaseStrategyInfo().getStatus() || StrategyStatusEnum.DISCARD.getType() == platformStrategyDO.getBaseStrategyInfo().getStatus())) {
            redisService.del(StrategyOnlineKeyBuild.strategyPlatformDetailKey(businessId));
            LambdaEsQueryWrapper<BasicStrategyEntity> wrapper = new LambdaEsQueryWrapper<>();
            wrapper.eq(BasicStrategyEntity::getMysqlId, businessId);
            strategyEntityMapper.delete(wrapper);
            return true;
        }
        //如果为发布则新增缓存和es
        if (updated && StrategyStatusEnum.PUBLISHED.getType() == platformStrategyDO.getBaseStrategyInfo().getStatus()) {
            PlatformStrategyDetailBO platformStrategyDetailBO = StrategyConvert.convertToPlatformStrategyDetailBO(platformStrategyDO);
            redisService.set(StrategyOnlineKeyBuild.strategyPlatformDetailKey(businessId), JSONObject.toJSONString(platformStrategyDetailBO));
            BasicStrategyEntity entity = StrategyConvert.buildBasicStrategyEntity(platformStrategyDetailBO);
            strategyEntityMapper.insert(entity);
        }
        return true;
    }
}
